require 'app/helpers/application_helper.rb'

# Filters added to this controller will be run for all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
  layout "standard"
  before_filter :set_logged_in_user
  session :secret => 'g89v89vvlkrlrjchch', :session_key => 'sharedkp_session'

  SUPERADMIN = 1
  OFFICER = 2
  MEMBER = 3

  def logged_in?
    if (@logged_in_user.nil? or @logged_in_user.id.nil?)
      return false
    else
      return true
    end
  end

  def is_rank?(rank)
    if logged_in?
      return (@logged_in_user.rank == rank)
    else
      return false
    end
  end

  helper_method(:is_superadmin?)
  def is_superadmin?
    # if no user exists, everyone is considered to be a superadmin until one is created
    is_rank?(SUPERADMIN) or (User.find(:first) == nil)
  end

  helper_method(:is_officer?)
  def is_officer?
    is_superadmin? or is_rank?(OFFICER)
  end

  helper_method(:is_member?)
  def is_member?
    is_officer? or is_rank?(MEMBER)
  end

  def set_logged_in_user
    if session[:logged_in_user_id]
      @logged_in_user = User.find(session[:logged_in_user_id])
    end

    return true
  end

  def require_logged_in_user
    logged_in?
  end

  def require_superadmin
    is_superadmin?
  end

  def require_officer
    is_officer?
  end

end
